
//
//  HPMainViewController.swift
//  MyWeibo
//
//  Created by apple on 16/4/6.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit

class HPMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加nav 控制器
        addChildViewControllers()

        replaceSystemTabBar()
 
    }
    
    // plusBtn 点击方法实现
    @objc private func plusBtnDidClick() {
        
    }
    
    //MARK: 使用kvc替换系统的tabbar
    private func replaceSystemTabBar() {
        
        let MyTabBar = HPMainTabbar()
        // 给按钮添加方法
        MyTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        setValue(MyTabBar, forKey: "tabBar")
        MyTabBar.backgroundColor = UIColor.blueColor()
    }
    
    // MARK: 添加子控制器
    private func addChildViewControllers() {
        addChildViewControllers(HPHomeViewController(), title: "首页", norlImage: "tabbar_home")
        addChildViewControllers(HPMessageViewController(), title: "消息", norlImage: "tabbar_message_center")
        addChildViewControllers(HPDiscViewController(), title: "发现", norlImage: "tabbar_discover")
        addChildViewControllers(HPMeViewController(), title: "我", norlImage: "tabbar_profile")
    }

    //MARK: 设置子控制器
    private func addChildViewControllers(vc :UIViewController, title :String, norlImage :String) {
        
//        homeVc.view.backgroundColor = UIColor.greenColor()
//        vc.tabBarItem.title = title
//        vc.navigationItem.title = title
        vc.title = title

        vc.tabBarItem.title = title
        vc.navigationItem.title = title

        vc.tabBarItem.image = UIImage(named: norlImage)
        vc.tabBarItem.selectedImage = UIImage(named: norlImage + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 设置文字不被渲染
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        // 创建导航控制器
        let nav = HPBaseNavController(rootViewController: vc)
        // 添加到控制器
        addChildViewController(nav)
    }

}
